package com.joyintech.modules.workflow.rest;

import com.joyintech.annotation.Log;
import com.joyintech.modules.workflow.service.WorkFlowService;
import com.joyintech.modules.workflow.service.dto.ModelQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 2.流程模型控制器
 * @author daiweiwei
 * @date 2018-12-03
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：流程管理->流程模型")
@RequestMapping("/api/workflow/model")
public class ProcessModelController {
    private final WorkFlowService workFlowService;
    //模型编辑，打开对应的编辑器


    @ApiOperation("流程模型")
    @GetMapping
    @PreAuthorize("@el.check('model:list')")
    public ResponseEntity<Object> query(ModelQueryCriteria criteria, Pageable pageable) throws Exception{
        return new ResponseEntity<>(workFlowService.listModel(criteria,pageable),HttpStatus.OK);
    }

    //部署
    @Log("流程部署")
    @ApiOperation("流程部署")
    @PostMapping("deploy")
    @PreAuthorize("@el.check('model:deploy')")
    public ResponseEntity<Object> deploy(String id) throws Exception {
        workFlowService.deployModel(id);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    //导出
    @Log("导出模型部署包")
    @ApiOperation("导出模型部署包")
    @PostMapping("download")
    @PreAuthorize("@el.check('model:download')")
    public ResponseEntity<Object> download(String id) throws Exception {
        workFlowService.downloadModel(id);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    //删除
    @Log("删除流程模型文件")
    @ApiOperation("删除流程模型文件")
    @DeleteMapping
    @PreAuthorize("@el.check('model:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids){
        workFlowService.deleteModel(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
